<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Controller extends CI_Controller {


    public function __construct() 
    {

        parent::__construct();
        $this->load->library('Arraytoxml');
        $this->load->helper('utility');
        $this->config->load('responsecodeconfig');
        $this->config->load('appidconfig');
        $this->ResponseCode = $this->config->item('ResponseCode');
        $this->AppConfig = $this->config->item('AppConfig');
        $last_update = time()-86400;
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0");
        header("Pragma: no-cache");
    }
    protected $params = array();
    protected $response_type = 'json';
    protected $response = array('code'=>9999,'message'=>'','data' => NULL);
    protected $ResponseCode = array();
  
    protected function __validation()
    {
        if($_SERVER['REQUEST_METHOD']==='GET')
        {
            $this->params = $_GET;
        }
        else if($_SERVER['REQUEST_METHOD']==='POST')
        {
            $this->params = $_POST;
        }
        else
        {
            $this->setResponse('METHOD_NOT_SUPPORT');
            $this->_print();
            exit;
        }

        if(array_key_exists('type',$this->params) && in_array(strtolower($this->params['type']),array('json','xml')))
        {
            $this->response_type = $this->params['type'];
        }
        $this->params = trim_space($this->params);

        if(!array_key_exists('appid',$this->params) || empty($this->params['appid']))
        {
            $this->setResponse('APPID_IS_EMPTY');
            $this->_print();
            exit;
        }
        $app = NULL;
        for($i = 0;$i<count($this->AppConfig);$i++)
        {
            if($this->AppConfig[$i]['appid']==$this->params['appid'])
            {
                $app = $this->AppConfig[$i];
                break;
            }
        }

        if($app==NULL)
        {

            $this->setResponse('APPID_IS_NOT_FOUND');
            $this->_print();
            exit;
        }
        if(!array_key_exists('sign',$this->params) || empty($this->params['sign']))
        {
            $this->setResponse('SIGN_EMPTY');
            $this->_print();
            exit;
        }
        ksort($this->params);
        $sign = $_SERVER['REQUEST_METHOD'];
        foreach($this->params as $key=>$value)
        {
            if($key=='sign' || $key=='appid') continue;
            $sign.=$key.$value;
        }

        $sign = md5(urldecode($sign).$app['security']);
//      if($sign!==$this->params['sign'])
//      {
//          $this->setResponse('SIGN_ERROR');
//          $this->_print();
//          exit;
//      }
        

        
    }
    protected function _print()
    {
        if(strtolower($this->response_type)=='json')
        {
            header('content-type:application/json;charset=utf-8');
            echo json_encode($this->response);
        }
        else
        {
            header('content-type:text/xml;charset=utf-8');
            echo $this->_toxml($this->response);
        }
    }
    
    protected function setResponse($code,$data = NULL)
    {
        $this->response = array('code'=>$this->ResponseCode[$code][0],'message'=>$this->ResponseCode[$code][1],'data' => $data!=NULL ? $data : NULL);
    }

    protected function _toxml($data)
    {
        return $this->arraytoxml->toXml($data,'response');
    }
}



